package com.jiandian.sheduler;

import com.jiandian.configration.OssConfig;
import com.jiandian.service.ApkService;
import com.jiandian.service.BannerService;
import com.jiandian.service.GameInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class SyncTask {

    @Autowired
    private GameInfoService gameInfoService;

    @Autowired
    private BannerService  bannerService;

    @Autowired
    private OssConfig ossConfig;

    @Autowired
    private ApkService apkService;

    @Scheduled(cron="0 0 * 2 * ?")
    public void syncGameInfo(){
        log.info("自动同步游戏信息");
        gameInfoService.syncAll(ossConfig.getBucketName());
    }

    @Scheduled(cron="0 0 * 2 * ?")
    public void syncBanner(){
        log.info("自动更新轮播图片");
        try {
            String urls = bannerService.fetchBannerUrls(ossConfig.getBucketName());
            bannerService.updateBanner(urls);
        }catch (Exception e){
            log.error(e.getMessage());
            throw new RuntimeException("自动更新轮播图片失败!");
        }
    }

    @Scheduled(cron="0 0 * 3 * ?")
    public void syncSelfApk(){
        log.info("自动更新selfapk");
        try {
            apkService.syncSelfApkUrl(ossConfig.getBucketName());
        }catch (Exception e){
            log.error(e.getMessage());
            throw new RuntimeException("自动更新selfapk失败!");
        }
    }
}
